/*************************************************************************
 * Tic Tac Toe
 *
 * Author: Justin Sheppard
 * Student #: 200811917
 * E-Mail: justin.sheppard@mun.ca
 * ***********************************************************************/
package tictactoe;

/** Board *****************************************************************
 * 
 * Immutable class used to deal with the game board.
 * 
 * ****************************************************************************/
public final class Board {
	private final char[][] board;
	
	/** Constructor *****************************************************************
	 * 
	 * Creates a new blank instance of board. Used only once per game for the initial board.
	 * @pre		-	
	 * @params	- 
	 * @returns - 
	 * @post	- A new Board object is created and the board member is filled with
	 * 			  spaces.
	 * ****************************************************************************/
	public Board(){
		board = new char[3][3];
		for (int r = 0; r < board.length; r++)
			for (int c = 0; c < board[r].length; c++)
				board[r][c] = ' ';
	}
	
	/** Constructor *****************************************************************
	 * 
	 * Creates a new board with the updated position of a performed move.
	 * @pre		-	
	 * @params	- other: The state of the current board.
	 * 			  move: The move made on the board.
	 * @returns - 
	 * @post	- A new board is created with the board member getting the old board
	 * 			  with the additional move added in.
	 * ****************************************************************************/
	public Board(Board other, Move move){
		board = other.board; //Copy the current board.
		board[move.getI()][move.getJ()] = move.getPiece(); //Set the position to the char.
	}
	
	/** toString *****************************************************************
	 * 
	 * Converts a char array of the printable board with piece updated piece positions
	 * to a string.
	 * 
	 * @pre		-	
	 * @params	- 
	 * @returns - A string representing the board char array.
	 * @post	- 
	 * ****************************************************************************/
	public String toString(){
		//The actual game board that gets printed to the console. The positions of board are placed
		//in their respective places.		  
		char gameBoard[][] = {	{'\t', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '\n'},
								{'\t', '|', ' ', board[0][0], ' ', '|', ' ', board[0][1], ' ', '|', ' ', board[0][2], ' ', '|', '\n'},
								{'\t', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '\n'},
								{'\t', '|', ' ', board[1][0], ' ', '|', ' ', board[1][1], ' ', '|', ' ', board[1][2], ' ', '|', '\n'},
								{'\t', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '\n'},
								{'\t', '|', ' ', board[2][0], ' ', '|', ' ', board[2][1], ' ', '|', ' ', board[2][2], ' ', '|', '\n'},
								{'\t', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'}
							};
		
		String temp = "";
		for (int r = 0; r < gameBoard.length; r++)
			for (int c = 0; c < gameBoard[r].length; c++)
				temp = temp + gameBoard[r][c];
		return temp;
	}
	
	/** get *****************************************************************
	 * 
	 * Gets the piece at the requested position.
	 * @pre		-	
	 * @params	- i: Row index
	 * 			  j: Col index
	 * @returns - Char at i,j
	 * @post	- 
	 * ****************************************************************************/
	public char get(int i, int j){
		return board[i][j];
		
	}
	
	/** isFull *****************************************************************
	 * 
	 * Tests if all positions are occupied by a piece.
	 * 
	 * @pre		-	
	 * @params	- 
	 * @returns - TRUE if all positions are occupied.
	 * 			  FALSE if there is a single position open (contains ' ' space)
	 * @post	- 
	 * ****************************************************************************/
	public boolean isFull(){
		for (int r = 0; r < board.length; r++)
			for (int c = 0; c < board[r].length; c++)
				if(board[r][c] == ' ')
					return false;
		return true;
		
	}
}
